package com.wuliang.generate.common.sharding.configs.datasource;

import com.wuliang.generate.common.sharding.factories.DataSourceConfigFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;

/**
 * @author WuLiang
 */
@Slf4j
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "systemEntityManagerFactoryBean",
        transactionManagerRef = "systemTransactionManager",
        basePackages = { "com.wuliang.generate.repositories.system" }
)
public class SystemDatasourceConfig extends DataSourceConfigFactory {

    /**
     *
     */
    @Autowired
    @Qualifier("systemDataSource")
    private DataSource systemDataSource;

    /**
     *
     */
    @Autowired
    @Qualifier("systemEntityManagerFactoryBuilder")
    private EntityManagerFactoryBuilder entityManagerFactoryBuilder;

    /**
     * non-javadoc {@see DataSourceConfigFactory#entityManagerFactoryBean}
     */
    @Override
    @Bean(name = "systemEntityManagerFactoryBean")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
        return entityManagerFactoryBuilder
                .dataSource(systemDataSource)
                .properties(jpaProperties.getProperties())
                .packages("com.wuliang.generate.bean.entities.system")
                .persistenceUnit("systemPersistenceUnit")
                .build();
    }

    /**
     * non-javadoc {@see DataSourceConfigFactory#entityManager}
     */
    @Override
    @Bean(name = "systemEntityManager")
    public EntityManager entityManager() {
        return entityManagerFactoryBean().getObject().createEntityManager();
    }

    /**
     * non-javadoc {@see DataSourceConfigFactory#transactionManager}
     */
    @Override
    @Bean(name = "systemTransactionManager")
    public JpaTransactionManager transactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());
        return transactionManager;
    }

    /**
     * non-javadoc {@see DataSourceConfigFactory#entityManagerFactoryBuilder}
     */
    @Override
    @Bean(name = "systemEntityManagerFactoryBuilder")
    public EntityManagerFactoryBuilder entityManagerFactoryBuilder() {
        return new EntityManagerFactoryBuilder(jpaVendorAdapter, jpaProperties.getProperties(), null);
    }

}
